package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.exception.Assert;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Wdd
 * @since 2021-09-05
 */
@Slf4j
@Api(tags = "积分等级管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
//@CrossOrigin
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService gradeService;

    @ApiOperation("积分等级列表")
     @GetMapping("/list")
     public R list(){
        log.info("hi is long info");
        log.warn("hi is long warn");
        log.error("hi is long error");
        List<IntegralGrade> list = gradeService.list();
        return  R.ok().data("list", list).message("获取列表成功");
    }
    @ApiOperation("根据id删除记录")
     @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(value = "数据id",example = "100",required = true)
            @PathVariable Long id){
        boolean result = gradeService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }
    @ApiOperation("新增积分等级")
    @PostMapping("/save")
   public R save(
           @ApiParam(value = "积分等级对象", required = true)
           @RequestBody IntegralGrade integralGrade) {
//        //如果借款额度为空就手动抛出一个自定义的异常！
//        if (integralGrade.getBorrowAmount() == null) {
//            //BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = gradeService.save(integralGrade);
        if (result){
            return R.ok().message("新增成功");
        }else {
            return R.error().message("新增失败");
        }
    }

    @ApiOperation("根据id获取id等级")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "数据id", required = true, example = "1")
            @PathVariable("id") Long id) {
        IntegralGrade integralGrade = gradeService.getById(id);
       if (integralGrade != null){
           return R.ok().data("record",integralGrade);
       }else {
           return R.error().message("数据获取失败");
       }
    }

    @ApiOperation("更新积分等级")
    @PostMapping("/update")
    public R updateById(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade) {

        boolean result = gradeService.updateById(integralGrade);
        if (result){
            return R.ok().message("更新成功");
        }else {
            return R.error().message("更新失败");
        }
    }
}

